package com.hxd.mall.widget

import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.view.View
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class HxdItemDecorationView(@param:LinearLayoutCompat.OrientationMode private val orientation: Int//方向
                            , private val decoration: Int//边距大小 px
                            , color: Int, private val leftPadding: Int) : RecyclerView.ItemDecoration() {
    private val mDivider: ColorDrawable?

    init {
        mDivider = ColorDrawable(color)
    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
        super.getItemOffsets(outRect, view, parent, state)
        val layoutManager = parent.layoutManager
        val lastPosition = state.itemCount - 1//整个RecyclerView最后一个item的position
        val current = parent.getChildLayoutPosition(view)//获取当前要进行布局的item的position
        if (current == -1) return //holder出现异常时，可能为-1
        if (layoutManager is LinearLayoutManager && layoutManager !is GridLayoutManager) {//LinearLayoutManager
            if (orientation == LinearLayoutManager.VERTICAL) {//垂直
                if (current == lastPosition) {
                    outRect.set(0, 0, 0, 0)
                } else {
                    outRect.set(0, 0, 0, decoration)
                }
            } else {//水平
                if (current == lastPosition) {
                    outRect.set(0, 0, 0, 0)
                } else {
                    outRect.set(0, 0, decoration, 0)
                }
            }
        }
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        if (orientation == LinearLayoutManager.VERTICAL) {//垂直
            drawHorizontalLines(c, parent)
        } else {//水平
            drawVerticalLines(c, parent)
        }
    }

    /**
     * 绘制垂直布局 水平的分割线
     */
    private fun drawHorizontalLines(c: Canvas, parent: RecyclerView) {
        val itemCount = parent.childCount
        val left = parent.paddingLeft + leftPadding
        val right = parent.width - parent.paddingRight
        for (i in 0 until itemCount) {
            val child = parent.getChildAt(i) ?: return
            val params = child.layoutParams as RecyclerView.LayoutParams
            val top = child.bottom + params.bottomMargin
            val bottom = top + 1
            mDivider!!.setBounds(left, top, right, bottom)
            mDivider.draw(c)
        }
    }

    /**
     * 绘制水平布局 竖直的分割线
     */
    private fun drawVerticalLines(c: Canvas, parent: RecyclerView) {
        val itemCount = parent.childCount
        val top = parent.paddingTop
        for (i in 0 until itemCount) {
            val child = parent.getChildAt(i) ?: return
            val params = child.layoutParams as RecyclerView.LayoutParams
            val bottom = child.height - parent.paddingBottom
            val left = child.right + params.rightMargin
            val right = left + 1
            if (mDivider == null) return
            mDivider.setBounds(left, top, right, bottom)
            mDivider.draw(c)
        }
    }
}